const config = require('../util/config')
const logger = require('log4js').getLogger()
const ChromeServerIterator = require('../mng/chromeServerMng').Iterator

module.exports = (req, res) => {
    require('../util/service').handlePost(req, res, (reqObj, resolve) => {
        if (config.domain == '') {
            require('../mng/chromeMng').loginTaobao().then(result => {
                logger.debug('[' + __filename + ':' + __line + ']', '登录成功')
                resolve({
                    result: 1,
                    fromUsername: config.local.taobaoUsername
                })
            }, result => {
                if (result)
                    logger.debug('[' + __filename + ':' + __line + ']', result)
                else
                    result = '系统错误'
                resolve({
                    result: -1,
                    msg: result,
                    fromUsername: config.local.taobaoUsername
                })
            }).catch(err => {
                logger.error('[' + __filename + ':' + __line + ']', err)
                resolve({
                    result: -1,
                    msg: '系统错误',
                    fromUsername: config.local.taobaoUsername
                })
            })
        } else if (config.domain == 'zoro.site') {
            var promises = []
            var chromeServerIterator = new ChromeServerIterator()
            while (1) {
                let chromeServer = chromeServerIterator.next()
                if (!chromeServer)
                    break
                promises.push(new Promise((resolve, reject) => {
                    var url = chromeServer + 'loginTaobao.do'
                    // logger.debug('[' + __filename + ':' + __line + ']', url)
                    require('../util/getHtml')({
                        url,
                        data: '{}'
                    }).then(result => {
                        // logger.debug('[' + __filename + ':' + __line + ']', url, result)
                        resolve(JSON.parse(result))
                    }, result => {
                        logger.error('[' + __filename + ':' + __line + ']', url, result)
                        resolve({
                            result: -1
                        })
                    })
                }))
            }
            if (promises.length == 0) {
                resolve({
                    result: 1
                })
                return
            }
            Promise.all(promises).then(result => {
                // logger.debug('[' + __filename + ':' + __line + ']', result)
                var successCnt = 0, failedCnt = 0, failedUsernames = []
                result.forEach(item => {
                    if (item.result == 1)
                        successCnt++
                    else {
                        failedCnt++
                        failedUsernames.push(item.fromUsername)
                    }
                })
                resolve({
                    result: 1,
                    successCnt,
                    failedCnt,
                    failedUsernames
                })
            }, result => {
                if (result)
                    logger.error('[' + __filename + ':' + __line + ']', result)
                else
                    result = '系统错误'
                resolve({
                    result: -1,
                    msg: result
                })
            }).catch(err => {
                logger.error('[' + __filename + ':' + __line + ']', err)
                resolve({
                    result: -1,
                    msg: '系统错误'
                })
            })
        }
    })
}
